<?php

class Cache {
	// SESSION-CACHE
	
	public static function IsCached($Group, $Phrase) {
		return ($_SESSION["cache"][$Group."_".md5($Phrase)]) ? true : false;
	}
	
	public static function Save($Group, $Phrase, $Data, $TTL = 60) {
		$_SESSION["cache"][$Group."_".md5($Phrase)]["data"] = serialize($Data);
		$_SESSION["cache"][$Group."_".md5($Phrase)]["time"] = time();
		$_SESSION["cache"][$Group."_".md5($Phrase)]["ttl"] = $TTL;
		
		return $Data;
	}
	
	public static function GetCache($Group, $Phrase) {
		//$timer = Timer::Dummy();
		
		if (!self::IsCached($Group, $Phrase)) return false;
		
		$Age = self::GetCacheAge($Group, $Phrase);
		
		if ($Age > $_SESSION["cache"][$Group."_".md5($Phrase)]["ttl"]) return false;
		
		//$_elapsed = $timer->End();	// in us
		
		return unserialize($_SESSION["cache"][$Group."_".md5($Phrase)]["data"]);
	}
	
	public static function ClearCacheGroup($Group) {
		if (!is_array($_SESSION["cache"])) return true;
		foreach ($_SESSION["cache"] as $_key => $_value) {
			if (substr($_key, 0, strlen($Group))==$Group) {
				unset($_SESSION["cache"][$_key]);
			}
		}
	}
	
	public static function GetCacheAge($Group, $Phrase) {
		if (!self::IsCached($Group, $Phrase)) return false;
		
		return time() - $_SESSION["cache"][$Group."_".md5($Phrase)]["time"];
	}
}

?>
